
package classes;

import components.CButton;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class PatientManagementPanel extends JPanel{

    PatientManagement patientController = new PatientManagement();
 
    Vector<String> header = new Vector<String>();
    Vector<String> patientList ;
    
    DefaultTableModel patientTableModel;
    JTable patientTable = new JTable();
   
    JLabel lblManage = new JLabel("Patient Management");
    JButton btnCancel = new JButton("Back", new ImageIcon(getClass().getResource("/images/cancel.png")));   
    JScrollPane scrollPane = new JScrollPane(patientTable);
    CButton btnAddPatient = new CButton("addpatient.png", 80, 80);
    CButton btnViewPatient = new CButton("viewpatient.png", 80,80);
    JLabel lblSearch = new JLabel("Search:");
    JTextField txtSearch = new JTextField("Search");
    JLabel lblFilter = new JLabel("Filter:");
    JComboBox selectFilter = new JComboBox(new String[] {"Display All", "First Name", "Last Name", "Address", "Age", "Gender"});
    boolean flag = false;
    
    public PatientManagementPanel()
    {
        //panel layout
        setLayout(null);
        setSize(740,655);
        setVisible(false);
        setOpaque(false);

        //add to panel
        add(lblManage);
        add(btnCancel);
        add(scrollPane);
        add(lblSearch);
        add(txtSearch);
        setButton(btnAddPatient, "addpatient2.png",20, 550);
        setButton(btnViewPatient, "viewpatient2.png",120, 550);
        add(lblFilter);
        add(selectFilter);
        txtSearch.setEnabled(false);
        
       
        //set font
        lblManage.setFont(new Font("Century Gothic", Font.BOLD, 16));
        btnCancel.setFont(new Font("Century Gothic", Font.BOLD, 14));
        patientTable.setFont(new Font("Century Gothic", Font.PLAIN, 14));
        lblSearch.setFont(new Font("Century Gothic", Font.BOLD, 14));
        txtSearch.setFont(new Font("Century Gothic", Font.PLAIN, 14));
        lblFilter.setFont(new Font("Century Gothic", Font.BOLD, 14));
        selectFilter.setFont(new Font("Century Gothic", Font.PLAIN, 14));
        txtSearch.setForeground(Color.GRAY);
        
       //setBounds
         lblManage.setBounds(20,20,250,30);
         btnCancel.setBounds(595, 600, 105, 30);
         scrollPane.setBounds(20,100,680,350);
         scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
         scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
         lblSearch.setBounds(480,70,100,30);
         txtSearch.setBounds(530,70,170,30);
         lblFilter.setBounds(280, 70, 100,30);
         selectFilter.setBounds(320, 70, 150, 30);
         
        txtSearch.addMouseListener(new MouseListener(){
        public void mouseClicked(MouseEvent e) {
            if(txtSearch.getText().equals("Search") && txtSearch.isEnabled())
            {
                txtSearch.setText(null);
                txtSearch.setForeground(Color.BLACK);
            }           
        }
        public void mousePressed(MouseEvent e) { }
        public void mouseReleased(MouseEvent e) { }
        public void mouseEntered(MouseEvent e) { }
        public void mouseExited(MouseEvent e) { }
        });
        txtSearch.addKeyListener(new KeyListener() {
            @Override
            public void keyPressed(KeyEvent e) {  
            }
            @Override
            public void keyTyped(KeyEvent e) {
                if(selectFilter.getSelectedIndex()!=0 && Main.frame.addPatient.isVisible()==false) 
                    searchPatientTable();
            }
            @Override
            public void keyReleased(KeyEvent e) {
                
            }
        
        });
        
        selectFilter.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(Main.frame.addPatient.isVisible()==false)
                {
                    if(selectFilter.getSelectedIndex()==0)
                    {
                       setPatientTable();
                       txtSearch.setEnabled(false);
                       txtSearch.setForeground(Color.GRAY);
                       txtSearch.setText("Search");
                       flag = false;
                    }
                    else if(selectFilter.getSelectedIndex()!=0 && flag == false)
                    {
                       txtSearch.setEnabled(true); 
                       flag =true;
                    }
                    else if(selectFilter.getSelectedIndex()!=0 && flag == true)
                    {
                       
                        searchPatientTable();
                        txtSearch.setEnabled(true);
                    }
                }
            }
            
        }
                
        );
        //initialize table model
         		 			 	 
        header.add("Patient No");
        header.add("Last Name ");
        header.add("First Name");
        header.add("Middle Name");
        header.add("Address");
        header.add("Age");
        header.add("Gender");
        header.add("Contact No");
        setPatientTable();
    }
   
     private void setButton(CButton button, String rollover, int x, int y)
    {
        add(button);
        button.setRollover(rollover);
        button.setLocation(x,y);
    }
     
     //checker if there is a selected item in the table
     public boolean isTableSelected(){
         if(patientTable.getSelectedColumn() != -1 && patientTable.getSelectedRow() != -1){
             return true;
         }
         else
             return false;
     }
     
       private JTable autoResizeColWidth(JTable table, DefaultTableModel model) {
        table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
        table.setModel(model);

        int margin = 5;

        for (int i = 0; i < table.getColumnCount()-1; i++) {
            int                     vColIndex = i;
            DefaultTableColumnModel colModel  = (DefaultTableColumnModel) table.getColumnModel();
            TableColumn             col       = colModel.getColumn(vColIndex);
            int                     width     = 0;

            // Get width of column header
            TableCellRenderer renderer = col.getHeaderRenderer();

            if (renderer == null) {
                renderer = table.getTableHeader().getDefaultRenderer();
            }

            Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);

            width = comp.getPreferredSize().width;

            // Get maximum width of column data
            for (int r = 0; r < table.getRowCount(); r++) {
                renderer = table.getCellRenderer(r, vColIndex);
                comp     = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false,
                        r, vColIndex);
                width = Math.max(width, comp.getPreferredSize().width);
            }

            // Add margin
            width += 2 * margin;

            // Set the width
            col.setPreferredWidth(width);
        }

        ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(
            SwingConstants.LEFT);

        // table.setAutoCreateRowSorter(true);
        table.getTableHeader().setReorderingAllowed(false);

        return table;
    }
     
     public void setPatientTable() {
      patientList = patientController.ViewAllPatient();
        patientTableModel = new DefaultTableModel(patientList, header)
                {
            @Override
                    public boolean isCellEditable(int row, int col)
                    {
                        return false;
                    }
                };
        
        patientTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        patientTable.setModel(patientTableModel);
        autoResizeColWidth(patientTable, patientTableModel);
     }
     
      public void searchPatientTable() {
      patientList = patientController.SearchPatient(selectFilter.getSelectedIndex(),txtSearch.getText());
        patientTableModel = new DefaultTableModel(patientList, header)
                {
            @Override
                    public boolean isCellEditable(int row, int col)
                    {
                        return false;
                    }
                };
        
        patientTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        patientTable.setModel(patientTableModel);
        autoResizeColWidth(patientTable, patientTableModel);
     }

}
